﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ATM
{
    public partial class Form1 : Form
    {
        ATM atm;

        public Form1()
        {
            InitializeComponent();
            
            atm = new ATM();
        }

        private void MainMenu_Click(object sender, EventArgs e)
        {
            this.radioButton1.Checked = false;
            this.radioButton2.Checked = false;
            this.radioButton3.Checked = false;//becareful with the control change
            this.InfoScreen.Clear();
            IsDeposit = false;
            IsWithdraw = false;
            if (atm.ReturnMainMenu())
            {
                this.InfoScreen.Text = "\r\nATM state is: " + atm.CurrentState.GetType().Name +
                                        "\r\nSelect account opptions from menu ==>";
                this.radioButton1.Enabled = true;
                this.radioButton2.Enabled = true;
                this.radioButton3.Enabled = true;
            }
            else
            {
                this.InfoScreen.Text = "\r\nATM state is: " + atm.CurrentState.GetType().Name +
                                        "\r\nCannot active the menu button at this state";
            }
            
         }

        private void Enter_Click(object sender, EventArgs e)
        {
            bool pass;
            int pin = 0;
            if(this.textBox2.Text != "")
                pin = int.Parse(this.textBox2.Text);

            pass = atm.EnterPIN(pin);

            if (pass)
            {
                this.InfoScreen.Clear();
                this.textBox2.Clear();
                this.textBox2.Enabled = false;
                this.InfoScreen.Text = "Grand access to your account" +
                                      "\r\nATM state is: " + atm.CurrentState.GetType().Name +
                                      "\r\nSelect account opptions from menu ==>" ;
                this.radioButton1.Enabled = true;
                this.radioButton2.Enabled = true;
                this.radioButton3.Enabled = true;
            }
            else if (!IsDeposit && !IsWithdraw)
            {
                this.InfoScreen.Clear();
                this.InfoScreen.Text = "Reenter PIN or get your card back" +
                                    "\r\nATM state is: " + atm.CurrentState.GetType().Name;
                this.textBox2.Clear();
            }

            if(IsDeposit)
            {

                double DepositeAmount = 0; 

                if (this.textBox3.Text != "")
                {
                    DepositeAmount = double.Parse(this.textBox3.Text);
                    this.textBox3.Text = "";
                }

                if (DepositeAmount != 0)
                {
                    atm.Deposit(DepositeAmount);
                }

                if (atm.GetAccount().Balance > 0)
                {
                    this.InfoScreen.Text = atm.GetAccount().Balance.ToString();

                    if (atm.GetAccount().State.GetType().Name != "WarningState")
                    {
                        this.InfoScreen.Text = "Your balane is: " + atm.GetAccount().Balance.ToString() +
                                            "\r\nThe account current state is: " + 
                                            atm.GetAccount().State.GetType().Name +
                                            "\r\nThe ATM current state is: " +
                                            atm.CurrentState.GetType().Name;
                    }
                    else
                    {
                        this.InfoScreen.Text = "Your balane is: " + atm.GetAccount().Balance.ToString() +
                                            "\r\nYou cannot withdraw when your account current state is: " + 
                                            atm.GetAccount().State.GetType().Name +
                                            "\r\nThe ATM current state is: " +
                                            atm.CurrentState.GetType().Name; 
                    }
                    this.InfoScreen.Text += "\r\n\nReturn to main menu - Press Main Menu button" +
                                            "\r\n\nGet your card back - Press eject card button";
                    this.textBox2.Clear();
                    this.textBox3.Enabled = false;
                    this.radioButton3.Enabled = false;
                }
            }

            if (IsWithdraw)
            {
                double WithdrawAmount = 0;
                if (this.textBox4.Text != "")
                {
                    WithdrawAmount = double.Parse(this.textBox4.Text);
                    this.textBox4.Text = "";
                }
                if (WithdrawAmount != 0)
                {
                    atm.Withdraw(WithdrawAmount);
                }
                if (atm.GetAccount().Balance > 0)
                {
                    this.InfoScreen.Text = atm.GetAccount().Balance.ToString();

                    if (atm.GetAccount().State.GetType().Name != "WarningState")
                    {
                        this.InfoScreen.Text = "Your balane is: " + atm.GetAccount().Balance.ToString() +
                                            "\r\nThe account current state is: " +
                                            atm.GetAccount().State.GetType().Name +
                                            "\r\nThe ATM current state is: " +
                                            atm.CurrentState.GetType().Name;
                    }
                    else
                    {
                        this.InfoScreen.Text = "Your balane is: " + atm.GetAccount().Balance.ToString() +
                                            "\r\nYou cannot withdraw when your account current state is: " +
                                            atm.GetAccount().State.GetType().Name +
                                            "\r\nThe ATM current state is: " +
                                            atm.CurrentState.GetType().Name;
                    }
                    this.InfoScreen.Text += "\r\n\nReturn to main menu - Press Return button" +
                                            "\r\n\nGet your card back - Press eject card button";
                    this.textBox2.Clear();
                    this.textBox3.Enabled = false;
                    this.radioButton3.Enabled = false;

                }
                this.textBox4.Enabled = false;
            }
            
        }

        private void InsertCard_Click(object sender, EventArgs e)
        {
            this.InfoScreen.Text = atm.InsertCard() + "\r\nATM state is: " + atm.CurrentState.GetType().Name;
            this.textBox2.Enabled = true;
           
        }

        private void EjectCard_Click(object sender, EventArgs e)
        {
            this.InfoScreen.Clear();
            IsDeposit = false;
            IsWithdraw = false;
            this.InfoScreen.Text = atm.EjectCard() + "\r\nATM state is: " + atm.CurrentState.GetType().Name;
            this.radioButton1.Checked = false;//becarefull with those check or uncheck since there will be state change
            this.radioButton2.Checked = false;
            this.radioButton3.Checked = false;
            this.textBox2.Enabled = false;
        }

        bool IsWithdraw;
        private void SelectCash(object sender, EventArgs e)
        {
            IsWithdraw = atm.SelectGetCash();
            if (IsWithdraw)
            {
                this.InfoScreen.Clear();
                this.InfoScreen.Text = "ATM state is: " + atm.CurrentState.GetType().Name +
                                    "\r\nPlease enter amount withdraw";
                this.radioButton2.Enabled = false;
                this.radioButton3.Enabled = false;
                this.textBox4.Enabled = true;
            }
        }

        private void SelectTransfer(object sender, EventArgs e)
        {
            atm.SelectTransfer();
        }

        bool IsDeposit;
        private void SelectDeposit(object sender, EventArgs e)
        {
            IsDeposit = atm.SelectDeposite();
            if (IsDeposit)
            {
                this.InfoScreen.Clear();
                this.InfoScreen.Text = "ATM state is: " + atm.CurrentState.GetType().Name +
                                    "\r\nPlease insert your deposite envelope";
                this.radioButton1.Enabled = false;
                this.radioButton2.Enabled = false;
                this.textBox2.Enabled = false;
            }
        }

        private void InsertEnvelope(object sender, EventArgs e)
        {
            if (atm.InsertEnvelope())
            {
                this.InfoScreen.Text = "ATM state is: " + atm.CurrentState.GetType().Name +
                                    "\r\nEnter deposit amount";
                this.radioButton3.Enabled = false;
                this.textBox3.Enabled = true;
                this.textBox2.Enabled = false;
            }
            else
            {
                this.InfoScreen.Text = "ATM state is: " + atm.CurrentState.GetType().Name +
                                    "\r\nCannot accept deposite envelope at this state";
                this.textBox2.Enabled = false;
            }
        }
    }
}
